package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scs.application.consts.SerialKeys;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.service.TreeServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.entity.Location;
import com.scs.application.modules.base.mapper.LocationMapper;
import com.scs.application.modules.base.service.LocationService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
public class LocationServiceImpl extends TreeServiceImpl<LocationMapper, Location> implements LocationService {

    @Override
    public boolean saveOrUpdate(Location entity) {
        if (entity.isNewRecord()) {
            entity.setCode(serialRuleAdapter.getSerialRuleService().generateCode(SerialKeys.BASE_LOCATION_CODE));
        }

        if(entity.getFlagDefault()) { //同一仓库下只能有一个默认库位
            if(super.list(new QueryWrapper<Location>()
            .eq("warehouse_id", entity.getWarehouseId())
            .eq("flag_default", "1")
            .ne("code", entity.getCode())).size() > 0) {
                throw new BusinessException("同一仓库下只能有一个默认库位");
            }
        }
//        if(StringUtils.isNotBlank(entity.getIdcode())){
//            Location location = super.getOne(Wrappers.<Location>query().eq("idcode",entity.getIdcode())
//                    .ne(StringUtils.isNotBlank(entity.getId()),"id",entity.getId())
//                    .last("limit 1"));
//            if(location != null) throw new BusinessException("您所设置的库位识别码【"+location.getIdcode()+"】已被【"+location.getName()+"】使用，请更换其他的");
//        }else {
//
//        }
        if (StringUtils.isNotBlank(entity.getType()) && StringUtils.isBlank(entity.getIdcode())) entity.setIdcode(entity.getCode());
        return super.saveOrUpdate(entity);
    }

    /**
     * 获取仓库的默认库位
     *
     * @param warehouseId
     * @return
     */
    @Override
    public Location getDefaultLocation(String warehouseId) {
        if (warehouseId == null) {
            Location location = new Location();
            location.setName("默认库位").setCode("Default").setId("0");
            return location;
        }
        List<Location> locationList = super.list(new QueryWrapper<Location>()
                .eq("warehouse_id", warehouseId)
                .eq("flag_default", "1"));
        if (locationList.size() > 0) {
            return locationList.get(0);
        } else {
            throw new BusinessException("没有设置默认库位");
        }
    }

    @Override
    public Location getLocationByCode(String code) {
        return getOne(Wrappers.<Location>query().eq("code",code));
    }

    @Override
    public Page<Map> pageForSelect(QueryContext context, Map<String, Object> userParam) {
        return this.baseMapper.pageForSelect(context.getPage(), context.getWrapper(), context.getParams(),userParam);
    }
}
